package cn.nami.web.frontpage.jdo;

import java.util.List;

import cn.nami.web.frontpage.entity.User;
import cn.nami.web.frontpage.jdo.core.CommonJdo;
import cn.nami.web.util.StringUtil;

public class UserJdo
{
	/**
	 * add a user to db
	 * @param user
	 */
	public static void add(User user)
	{
		if(user == null)
		{
			return;
		}
		CommonJdo.save(user);
	}
	
	/**
	 * get a user by id
	 */
	public static User get(Integer id)
	{
		if(id == null)
		{
			return null;
		}
		return CommonJdo.get(User.class, id);
	}
	
	/**
	 * check a user is esist
	 */
	public static boolean isExist(String username)
	{
		if(StringUtil.isEmpty(username))
		{
			return false;
		}
		User user = getByName(username);
		return user != null;
	}
	
	/**
	 * get a user by user name;
	 */
	public static User getByName(String username)
	{
		List<User> userList = CommonJdo.list(User.class, "username = '" + username + "'");
		if(userList == null || userList.size() <= 0)
		{
			return null;
		}
		return userList.get(0);
	}
}